#coding=utf-8
import json
import sys
import os
import moviepy.editor as mpy
res_path=os.getcwd()

startMinute=0
startSecond=0
endMinute=0
endSecond=0
width=300
height=300


def mp4ToGif(path):
	outPath=os.path.join(res_path,"out")
	index0=path.rfind('\\')
	index1=path.rfind('.')
	name=path[index0+1:index1]

	outPath=os.path.join(outPath,name+".gif")
	
	print(outPath)
	#视频文件的本地路径
	content = mpy.VideoFileClip(path)
	# 剪辑0分0秒到0分4秒的片段。resize为修改清晰度
	c1 = content.subclip((int(startMinute),int(startSecond)),(int(endMinute),int(endSecond))).resize((int(width),int(height)))
	# 将片段保存为gif图到python的默认路径
	c1.write_gif(outPath)

def getPngFile():
	path=os.path.join(res_path,"res")
	list = os.listdir(path)
	for dir in list:
		dir_path=os.path.join(path,dir)   # 遍历的全路径
		if(os.path.isfile(dir_path)):   #如果是文件存储
			index=dir_path.rfind(".")
			suffix=dir_path[index+1:]
			if suffix=="mp4":
				mp4ToGif(dir_path)

if __name__=="__main__":
	startMinute=raw_input("please input start minute:")  
	startSecond=raw_input("please input start second:")  
	endMinute=raw_input("please input end minute:") 
	endSecond=raw_input("please input end second:")  
	width=raw_input("please input width:")  
	height=raw_input("please input height:")  
	getPngFile()



